{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modeling HIV infection"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*Modeling and Simulation in Python*\n",
    "\n",
    "Copyright 2021 Allen Downey\n",
    "\n",
    "License: [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-nc-sa/4.0/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# install Pint if necessary\n",
    "\n",
    "try:\n",
    "    import pint\n",
    "except ImportError:\n",
    "    !pip install pint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# download modsim.py if necessary\n",
    "\n",
    "from os.path import basename, exists\n",
    "\n",
    "def download(url):\n",
    "    filename = basename(url)\n",
    "    if not exists(filename):\n",
    "        from urllib.request import urlretrieve\n",
    "        local, _ = urlretrieve(url, filename)\n",
    "        print('Downloaded ' + local)\n",
    "    \n",
    "download('https://github.com/AllenDowney/ModSimPy/raw/master/modsim.py')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# import functions from modsim\n",
    "\n",
    "from modsim import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "During the initial phase of HIV infection, the concentration of the virus in the bloodstream typically increases quickly and then decreases.\n",
    "The most obvious explanation for the decline is an immune response that destroys the virus or controls its replication.\n",
    "However, at least in some patients, the decline occurs even without any detectable immune response.\n",
    "\n",
    "In 1996 Andrew Phillips proposed another explanation for the decline (\"Reduction of HIV Concentration During Acute Infection: Independence from a Specific Immune Response\", available from <https://people.math.gatech.edu/~weiss/uploads/5/8/6/1/58618765/phillips1996.pdf>).\n",
    "\n",
    "Phillips presents a system of differential equations that models the concentrations of the HIV virus and the CD4 cells it infects.\n",
    "The model does not include an immune response; nevertheless, it demonstrates behavior that is qualitatively similar to what is seen in patients during the first few weeks after infection.\n",
    "\n",
    "His conclusion is that the observed decline in the concentration of HIV might not be caused by an immune response; it could be due to the dynamic interaction between HIV and the cells it infects.\n",
    "\n",
    "In this notebook, we'll implement Phillips's model and consider whether it does the work it is meant to do."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Model\n",
    "\n",
    "The model has four state variables, `R`, `L`, `E`, and `V`.  Read the paper to understand what they represent.\n",
    "\n",
    "Here are the initial conditional we can glean from the paper. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "init = State(R=200, L=0, E=0, V=4e-7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The behavior of the system is controlled by 9 parameters.\n",
    "That might seem like a lot, but they are not entirely free parameters; their values are constrained by measurements and background knowledge (although some are more constrained than others).\n",
    "Here are the values from Table 1.\n",
    "\n",
    "Note: the parameter $\\rho$ (the Greek letter \"rho\") in the table appears as $p$ in the equations.  Since it represents a proportion, we'll use $p$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "gamma = 1.36\n",
    "mu = 1.36e-3\n",
    "tau = 0.2\n",
    "beta = 0.00027\n",
    "p = 0.1\n",
    "alpha = 3.6e-2\n",
    "sigma = 2\n",
    "delta = 0.33\n",
    "pi = 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here's a `System` object with the initial conditions and the duration of the simulation (120 days).\n",
    "Normally we would store the parameters in the `System` object, but the code will be less cluttered if we leave them as global variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "system = System(init=init,\n",
    "                t_end=120,\n",
    "                num=481)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** Use the equations in the paper to write a slope function that takes a `State` object with the current values of `R`, `L`, `E`, and `V`, and returns their derivatives in the corresponding order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test your slope function with the initial conditions.\n",
    "The results should be approximately\n",
    "\n",
    "```\n",
    "-2.16e-08, 2.16e-09, 1.944e-08, -8e-07\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** Now use `run_solve_ivp` to simulate the system of equations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Solution goes here"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next few cells plot the results on the same scale as the figures in the paper.\n",
    "\n",
    "**Exericise:** Compare your results to the results in the paper.\n",
    "Are they consistent?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.V.plot(label='V')\n",
    "decorate(xlabel='Time (days)',\n",
    "         ylabel='Free virions V',\n",
    "         yscale='log',\n",
    "         ylim=[0.1, 1e4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.R.plot(label='R', color='C1')\n",
    "decorate(xlabel='Time (days)',\n",
    "         ylabel='Number of cells',\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.L.plot(color='C2', label='L')\n",
    "results.E.plot(color='C4', label='E')\n",
    "decorate(xlabel='Time (days)',\n",
    "         ylabel='Number of cells',\n",
    "         yscale='log',\n",
    "         ylim=[0.1, 100])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** What kind of work is this model doing?  Do you find the results convincing?  What are the strengths of the model?  What are the weaknesses?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Exercise:** Read [this response to Phillips's article](https://science.sciencemag.org/content/sci/272/5270/1960.full.pdf) and Phillips's response to the response.  What do you think of the arguments on both sides.  Do you think the model Phillips proposed is sufficient to make his argument, or do you think it leaves out essential features of the real world?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
